/*
 * @Author: xuesong
 * @Date: 2021-12-01 17:37:20
 * @LastEditors: xuesong
 * @LastEditTime: 2022-06-02 16:26:51
 * @FilePath: \XCH-C202_Remate-V01\projects\ble_mulit\simpleBleMultiConnection\src\Remote\AdcMeasure.c
 */

#include "clock.h"
#include "halPeripheral.h"
#include "BatteryMeasure.h"
// #include "simpleBLEPeripheral.h"
#include "pwrmgr.h"
#include "adc.h"
// #include "user_Bz.h"
// #include "machine.h"

//---------------------------------------------
#define TEMP_BAT 	ADC_CH1P_P23 // ADC_CH2P_P14//
//---------------------------------------------
AdcCfg_t adc_cfg = {
	.channel_flag = ADC_BIT(TEMP_BAT),
	.is_continue_mode = FALSE,
	.is_high_resolution = 0xBF,
};

Battery_Str_t Battery_t = {
	.Flag.flagByte = 0,
};
//---------------------------------------------
#define MAX_SAMPLE_POINT 64
uint16_t adc_debug[6][MAX_SAMPLE_POINT];
//---------------------------------------------
#if (PRV_ADC_TEST == 0)
static uint8_t channel_done_flag = 0;
#endif
//---------------------------------------------
static void adc_evt(AdcEvt_t *pev);
//---------------------------------------------
void UserMeasureTask(AdcCfg_t tempAdcCfg)
{
	int ret;
	// bool batt_mode = FALSE;//TRUE;
	// uint8_t batt_ch = TEMP_BAT;
	// GpioPin_t pin;

	// LOG("start user measure adc\n");
	osal_memcpy(&adc_cfg, &tempAdcCfg, sizeof(tempAdcCfg));

	ret = HalAdcChannelConfig(adc_cfg, adc_evt);
	if (ret)
	{
		LOG("ret = %d\n", ret);
		HalAdcInit();
		return;
	}
	subWriteReg(0x4000F000 + 0x7c, 3, 3, 0);
	HalAdcStart();
}

void FunUserADC(void)
{
// BAT------1200K-----240K----GND
#define D_BAT_RH 120
#define D_BAT_RL 24
// BAT------2700K-----680K----GND
// #define D_BAT_RH 270
// #define D_BAT_RL 68
#define D_BAT_DIV D_BAT_RL
#define D_BAT_MUL (D_BAT_RL + D_BAT_RH)
// #define D_BAT_VLO 	32  // 3.3V
// #define D_BAT_FULL 	38 // 4.2V
uint8_t batteryLowFlag = 0;
#define B_BatteryLow_Li		0x01
#define B_BatteryLow_mask	(B_BatteryLow_Li)
	static uint8 AD_Times = 0;
	uint32 batMv = 0;
	// LOG("user adc\n");
	if (!(Battery_t.Flag.flagBit.OK))
	{
		if (!(Battery_t.Flag.flagBit.init))
		{
			// Battery_t.flag |= D_BAT_Init;
			// Battery_t.flag &= (~D_BAT_Av1st);
			Battery_t.Flag.flagBit.init = 1;
			Battery_t.Flag.flagBit.Av1st = 0;
			AD_Times = 0;
			// hal_pwrmgr_lock(MOD_GPIO);
            // vMacExitLowPower();
			UserMeasureTask(adc_cfg);
		}

		if(Battery_t.Flag.flagBit.AdOK)
		{
			Battery_t.Flag.flagBit.AdOK = 0;
			AD_Times++;
			if (AD_Times > 2)
			{
				if(Battery_t.Flag.flagBit.Ad0ok)
				{
					Battery_t.Flag.flagBit.Ad0ok = 0;
					if (adc_cfg.is_high_resolution & BIT(TEMP_BAT))
					{
						batMv = (Battery_t.BateryVolAD[0] * D_BAT_MUL) / D_BAT_DIV;
						batMv += 150;	//强制上浮 0.15V
					}
					else
					{
						batMv = Battery_t.BateryVolAD[0];
					}
					// LOG("in %dmv--realy %dmv\r\n",Battery_t.BateryVolAD[0],batMv);
					batMv /= 50;
					batMv ++;
					batMv >>= 1;
					if(Battery_t.Flag.flagBit.Av1st)
					{
						batMv += Battery_t.BateryVoltage[0];
						batMv >>= 1;
					}
					Battery_t.BateryVoltage[0] = batMv;						//手动修正增加 0.2V
					// Battery_t.BateryVoltage = batMv / 50; //< /100 4out5inc
					// Battery_t.BateryVoltage++;
					// Battery_t.BateryVoltage /= 2; 		  //>
				}
				Battery_t.Flag.flagBit.Av1st = 1;
				if(AD_Times > 5)
				{
					// LOG("li BAT 0.1V = %d\r\n", Battery_t.BateryVoltage[0]);
					Battery_t.Flag.flagBit.LO = 0;
					if (Battery_t.BateryVoltage[0] <= D_BAT_VLO)
					{
						batteryLowFlag |= B_BatteryLow_Li;
						Battery_t.BateryVoltage[0] = 0;
					}
					else
					{
						batMv = (Battery_t.BateryVoltage[0] - D_BAT_VLO) * 100;
						batMv = batMv / (D_BAT_FULL - D_BAT_VLO);
						if (batMv > 100)
							batMv = 100;
						Battery_t.BateryVoltage[0] = batMv;
						// LOG("li BAT = %d%%\r\n", Battery_t.BateryVoltage[0]);
					}
					if((batteryLowFlag & B_BatteryLow_mask) == B_BatteryLow_mask)
					{
						Battery_t.Flag.flagBit.LO = 1;
						Battery_t.BatteryLevle = 0;
					}else{
						Battery_t.BatteryLevle = MAX(Battery_t.BateryVoltage[0],Battery_t.BateryVoltage[1]);
					}
					AD_Times = 0;
					// Battery_t.flag |= D_BAT_OK;
					Battery_t.Flag.flagBit.OK = 1;
					Battery_t.Flag.flagBit.Finish1S = 1;
				}
			}
			
			if(!Battery_t.Flag.flagBit.OK)
			{
				UserMeasureTask(adc_cfg);
			}
			// Battery_t.flag &= (~D_BAT_ADOKMASK);
		}

		if(Battery_t.Flag.flagBit.OK)
		{
			// hal_pwrmgr_unlock(MOD_GPIO);
            // vMacEnterLowPower();
		}
	}
}

static void adc_evt(AdcEvt_t *pev)
{
	int32_t value = 0;
	int32_t voltage = 0;
	int i = 0;
	bool is_high_resolution = FALSE;
	// bool is_differential_mode = FALSE;
	uint8_t ch = 0;

	// LOG("CHN: %X\r\n",pev->ch);

	if ((pev->type != HAL_ADC_EVT_DATA) || (pev->ch < 2))
		return;
	osal_memcpy(adc_debug[pev->ch - 2], pev->data, 2 * (pev->size));
	channel_done_flag |= BIT(pev->ch);

	if (channel_done_flag == adc_cfg.channel_flag)
	{
		for (i = 2; i < 8; i++)
		{
			if (channel_done_flag & BIT(i))
			{
				is_high_resolution = (adc_cfg.is_high_resolution & BIT(i)) ? TRUE : FALSE;
				// is_differential_mode = (adc_cfg.is_differential_mode & BIT(i)) ? TRUE : FALSE;
				value = HalAdcValueCal((AdcChannel_t)i, adc_debug[i - 2], pev->size, is_high_resolution);//, is_differential_mode);
				switch (i)
				{
				case ADC_CH1N_P11:
					ch = 11;
					break;
				case ADC_CH1P_P23:
					ch = 23;
					break;
				case ADC_CH2N_P24:
					ch = 24;
					break;
				case ADC_CH2P_P14:
					ch = 14;
					break;
				case ADC_CH3N_P15:
					ch = 15;
					break;
				case ADC_CH3P_P20:
					ch = 20;
					break;
				default:
					break;
				}

				if (ch != 0)
				{
					voltage = (value);
					// voltage = (int16_t)(value * 1000);
					// LOG("P%d %d mv \r\n", ch, voltage);
					if (i == TEMP_BAT)
					{
						Battery_t.BateryVolAD[0] = voltage;
						Battery_t.Flag.flagBit.Ad0ok = 1;
					}
					else
					{
						LOG("undefine channel\n");
					}
				}
				else
				{
					LOG("invalid channel\n");
				}
			}
		}
		// if(Battery_t.Flag.flagBit.Ad0ok && Battery_t.Flag.flagBit.Ad1ok)
		if(Battery_t.Flag.flagBit.Ad0ok)
			Battery_t.Flag.flagBit.AdOK = 1;

		channel_done_flag = 0;
		if (adc_cfg.is_continue_mode == FALSE)
		{
			// peripheral_task_set(USER_TASK_START,USR_EVT_ADC,50);
			// hal_pwrmgr_unlock(MOD_GPIO);
		}
	}
}
